﻿/*
 * Copyright 2009 José David Tuero Rodríguez

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 * */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace AtmosCreator
{
    /// <summary>
    /// Form for configurate "the game"
    /// </summary>
    public partial class FrmConf : Form
    {
        /// <summary>
        /// Form father of this
        /// </summary>
        private Form _father;

        /// <summary>
        /// Instance of configuration class
        /// </summary>
        private Configuration _configuration;

        /// <summary>
        /// Localizer
        /// </summary>
        Localizer _localizer;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="father">The father form of this</param>
        /// <param name="localizer">Localizer</param>
        /// <param name="game">Game used</param>
        public FrmConf(Form father, Localizer localizer,string game)
        {
            _father = father;
            _localizer = localizer;
            InitializeComponent();
            localizer.LocalizeForm(this);
            try
            {
                _configuration = new Configuration(game);
            }
            catch (Exception)
            {
                MessageBox.Show(localizer.GetError("CreatingDefaultConfigurationFile"), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                try
                {
                    Configuration.CreateDefaultConfig(game);
                    _configuration = new Configuration(game);
                }
                catch (Exception)
                {
                    MessageBox.Show(localizer.GetError("CreateDefaultConfigurationFileError"), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    Application.Exit();
                }
            }
            //Bind the controls to theirs values
            trbSeconds.DataBindings.Add(new Binding("Value", _configuration, "VidSeconds"));
            trbVideo.DataBindings.Add(new Binding("Value", _configuration, "VidPer"));
            trbGameTime.DataBindings.Add(new Binding("Value", _configuration, "GameTime"));
            trbMinChosen.DataBindings.Add(new Binding("Value", _configuration, "MinChosen"));
            trbMaxChosen.DataBindings.Add(new Binding("Value", _configuration, "MaxChosen"));
            trbPerChosen.DataBindings.Add(new Binding("Value", _configuration, "PerChosen"));

            txtSeconds.DataBindings.Add(new Binding("Text", trbSeconds, "Value"));
            txtVideo.DataBindings.Add(new Binding("Text", trbVideo, "Value"));
            txtGameTime.DataBindings.Add(new Binding("Text", trbGameTime, "Value"));
            txtMinChosen.DataBindings.Add(new Binding("Text", trbMinChosen, "Value"));
            txtMaxChosen.DataBindings.Add(new Binding("Text", trbMaxChosen, "Value"));
            txtPerChosen.DataBindings.Add(new Binding("Text", trbPerChosen, "Value"));
            
            chkStretch.DataBindings.Add(new Binding("Checked", _configuration, "Stretch"));
        }




       
        /// <summary>
        /// Save the configuration to hard disk
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            _configuration.GameTime = trbGameTime.Value;
            _configuration.MaxChosen = trbMaxChosen.Value;
            _configuration.MaxChosen = trbMaxChosen.Value;
            _configuration.MinChosen = trbMinChosen.Value;
            _configuration.PerChosen = trbPerChosen.Value;
            _configuration.Stretch = chkStretch.Checked;
            _configuration.VidPer = trbVideo.Value;
            _configuration.VidSeconds = trbSeconds.Value;
            try
            {
                _configuration.Save();
            }
            catch (Exception)
            {
                MessageBox.Show(_localizer.GetError("CreateDefaultConfigurationFileError"), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                Application.Exit();
            }
        }


        /// <summary>
        /// When closing we make visible the father form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmConf_FormClosed(object sender, FormClosedEventArgs e)
        {
            _father.Visible = true;
        }








    }
}
